package com.lagou.edu.transfer.conf;

import com.lagou.edu.spring.annotations.Autowired;
import com.lagou.edu.spring.annotations.Bean;
import com.lagou.edu.spring.annotations.Component;
import com.lagou.edu.spring.mybatis.MapperBeanParseProcess;
import com.lagou.edu.spring.mybatis.transaction.MyTransactionalParseProcess;
import com.lagou.niancheng.core.parse.Resources;
import com.lagou.niancheng.core.session.SqlSessionFactory;
import com.lagou.niancheng.core.session.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;

import javax.annotation.PostConstruct;
import java.beans.PropertyVetoException;

/**
 * 持久层相关配置
 * 通过Import引入的两个依赖相当于@MapperScan 和 @EnableTransactionManager
 *
 * @author 大犬
 * @date 2020/4/3
 */
@Component
public class MybatisConf {

    @PostConstruct
    public void init() {
        System.out.println("加载mybatis配置");
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws PropertyVetoException, DocumentException {
        System.out.println("加载sqlSessionFactory");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        return sqlSessionFactoryBuilder.build(Resources.getResourceAsSteam("sqlMapConfig.xml"));
    }

    @Bean
    public MapperBeanParseProcess mapperBeanParseProcess(@Autowired("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        System.out.println("执行MapperScan");
        return new MapperBeanParseProcess(sqlSessionFactory);
    }

    @Bean
    public MyTransactionalParseProcess myTransactionalParseProcess(@Autowired("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        System.out.println("开启事务管理器");
        return new MyTransactionalParseProcess(sqlSessionFactory);
    }
}
